package com.ly.ql.controller;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ly.ql.Entity.MaterialEntity;
import com.ly.ql.Result.Response.Response;
import com.ly.ql.RocketMqListener.Produce.ProduceUtils;
import com.ly.ql.Wms.Service.InStockBillPushService;
import com.ly.ql.Wms.utils.LoginUtils;
import com.ly.ql.service.MaterialService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.*;

@RestController
@RequiredArgsConstructor
@RequestMapping("/Material")
public class MaterialController {

    private final MaterialService materialService;
    private final ProduceUtils produceUtils;
    private final LoginUtils loginUtils;
    private final InStockBillPushService inStockBillPushService;
    @GetMapping("/selectAllMaterial")
    public Response selectMaterialList(HttpServletRequest request)  {
        System.out.println(request.getMethod());
//        List<MaterialEntity> materialEntities = materialService.selectMaterialEntity();
//        for (MaterialEntity materialEntity : materialEntities) {
//            String str = JSONObject.toJSONString(materialEntity, SerializerFeature.DisableCircularReferenceDetect);
//            Map<String,Object> map =new HashMap<>();
//            map.put("status",200);
//            produceUtils.sendMessage(str,map,materialEntity.getOID());
//        }
        String login = null;
        inStockBillPushService.pushTransferIn();
        Response result = new Response("success", 200,"");
        return result;
    }

    @PostMapping(value = {"/selectByCondition/{code}/{warehouseCode}","/selectByCondition/{code}"})
    public Response selectMaterialInfoByCodeAndWare(@PathVariable() String code,@PathVariable(required = false,value = "HT") String warehouseCode){
        List<MaterialEntity> materialEntities = materialService.selectMaterialByCodeAndWareHouseCode(code, warehouseCode);
        LinkedList<String> list =new LinkedList<>();
        list.add("1创建");
        list.add("2审核");
        list.add("3付款");
        list.add("4收货");
        list.add("5创建");
        list.add("6审核");
        list.add("7付款");
        list.add("8收货");

//        if (materialEntities!=null&&!materialEntities.isEmpty()){
//            ObjectMapper mapper =new ObjectMapper();
//               materialEntities.forEach(item->{
//                   Map<String,Object> map =new HashMap<>();
//                   map.put("status",200);
//                   String str="";
//                   try {
//                        str= mapper.writeValueAsString(item);
//                   } catch (JsonProcessingException e) {
//                       map.put("status",500);
//                       str="code="+item.getCode();
//                   }
//                   produceUtils.sendMessageOrder(str,map,item.getOID());
//               });
//        }
        list.forEach(item->{
            Response response = produceUtils.sendMessageOrder(item, null, 3L);
            if (response.getCode()==200){
                System.out.println("发送成功");
            }
        });
        Response result = new Response("success",200,materialEntities);
        return result;
    }
}
